//@Description:将员工管理页面的1千200多行js拆出来，
//@Date 2017年7月3日16:37:55
//@Author:shuyangyang

/**
 * 初始化页面控件
 */
var isOnlyOne = false;
$(function() {
	if (isFlag == "1") {
		var eStateId = $("#eState").val();
		var eStateText = $("#eState").find("option:selected").text();
		$("#eState").remove();
		var eStatehtml = "<input type='hidden' id='eState' name='eState' value='" + eStateId + "'/>";
		eStatehtml += "<span>" + eStateText + "</span>";
		$("#eStatus").append(eStatehtml);
	} else {
		$("#eState").select2({
			placeholder : "请选择"
		});
	}
	$("#eSex").select2({
		placeholder : "请选择性别"
	});
	if (hireIsOnly == "1") {
		$("#eRegTime").attr("disabled", "disabled");
		$("#eHireDate").attr("disabled", "disabled");
	} else {
	}
	if (isReg == "1") {
		if (eTime != null && eTime != "") {
			$("#eRegTime").attr("disabled", "disabled");
		} else {
			$("#eRegTime").removeAttr("disabled");
		}
	}
	if (isFlag == 1) {
		$("#saveAll").hide();
		$("#saveJb").show();
		$("#saveEd").show();
		$("#saveWk").show();
		$("#saveFy").show();
	} else {
		$("#saveAll").show();
		$("#saveJb").hide();
		$("#saveEd").hide();
		$("#saveWk").hide();
		$("#saveFy").hide();
	}
	// 学历下拉
	initED(graduationInfoList);
	$("*[name='eDegree']").select2({
		placeholder : "请选择学历"
	});
	// 开户银行下拉
	initOpenBank(selectedBank);
	$("#bank0[name='bank']").select2({
		placeholder : "请选择开户银行"
	});
	// 关系下拉
	initFY(relate);
	$("*[name='eFamRelation']").select2({
		placeholder : "请选择与本人关系"
	});
	// 性别单选
	initSex(relate);
	$("*[name='eFamSex']").select2({
		placeholder : "请选择性别"
	});
	$("#cSelect2").select2();

	$("#pcName").hide();
	$.ajax({
		url : "/IMS/select/getCompanyListByPcid.ht?pcid=" + pcid,
		dataType : 'json',
		beforeSend : function() {
			showProccessBar();
		},
		success : function(result) {
			if (result != null && result.total != null && result.total != undefined) {
				if (result.total == 1) {
					isOnlyOne = true;// 只有一个公司可选
					var data = result.list;
					$("#pcidShowOrHide").remove();
					$("#pcName").show();
					$("#pcName").html(data[0].name);
					$("#companyName").val(data[0].name);
					$("#companyId").val(data[0].id);
					// 不是编辑状态，初始化小区
					if (isFlag != "1") {
						$.ajax({
							url : '/IMS/select/serchComm.ht',
							data : {
								pcid : pcid
							},
							success : function(data) {
								if (data.code == 0) {
									if (data.result && data.result.length > 0) {
										$.each(data.result, function(index) {
											var dom = "<option value='" + this.cid + "' >" + this.communityName + "</option>";
											$("#cSelect2").append(dom);
										});
										$("#cSelect2").select2({
											placeholder : "请选择小区"
										});
										changeCommunity();
									} else {
										$("#cSelect2").select2({
											placeholder : "该公司尚未录入小区",
											enable : false
										});
										changeCommunity();
									}
									hideProccessBar();
								}
							},
							error : function() {
								$("#cSelect2").select2({
									placeholder : "加载失败了，请重新打开页面",
									enable : false
								});
								hideProccessBar();
							}
						});
					} else {
						changeCommunity();
						hideProccessBar();
					}
				} else {
					$("#pcidShowOrHide").show();
					$("#onlyonePcid").remove();
					initCompany();
					changeCommunity();
					hideProccessBar();
				}
			} else {
				hideProccessBar();
			}
		},
		error : function() {
			$("#pcidShowOrHide").show();
			$("#onlyonePcid").remove();
			initCompany();
			changeCommunity();
			hideProccessBar();
		}
	});

	$("#select2-cpSelect2-container").html(companyName);
	setValidate($("#frm"), {
		rules : {
			startDate : {
				required : true,
				afterDate : function() {
					return $("#endDate").val();
				}
			},
			endDate : {
				required : true,
				beforeDate : dateToStr(new Date())
			}
		},
		messages : {
			startDate : {
				required : "请选择起始时间",
				afterDate : "起始日期不能大于结束日期"
			},
			endDate : {
				required : "请选择结束时间",
				beforeDate : "结束日期不能小于当天"
			}
		}
	});

	$("#cpSelect2").change(function() {
		$("#cSelect2").children().remove();
		$.ajax({
			url : '/IMS/select/serchComm.ht',
			data : {
				pcid : $("#cpSelect2").val()
			},
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data) {
				if (data.code == 0) {
					if (data.result && data.result.length > 0) {
						$("#cSelect2").append("<option></option>")
						$.each(data.result, function(index) {
							var dom = "<option value='" + this.cid + "' >" + this.communityName + "</option>";
							$("#cSelect2").append(dom);
						});
						$("#cSelect2").select2({
							placeholder : "请选择小区"
						});
					} else {
						$("#cSelect2").select2({
							placeholder : "该公司尚未录入小区",
							enable : false
						});
					}
					changeCommunity();
				}
				hideProccessBar();
			}
		});
	});

	$("#bank0").change(function() {
		var bankName = $("#bank0").find("option:selected").text();
		$("#bankName").val(bankName);
	});
	$("#dSelect2").select2({
		placeholder : "请选择部门"
	});
	$("#pSelect2").select2({
		placeholder : "请选择职位"
	});
});

// 初始化公司控件
function initCompany() {
	$("#cpSelect2").select2({
		placeholder : "请选择物业公司",
		language : "zh-CN",
		ajax : {
			url : "/IMS/select/getCompanyListByPcid.ht?pcid=" + pcid,
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,
					// 输入的内容，会发给服务器进行查找
					// 以下两行可以在数据量大的时候用到（当下拉框往下拉的时候会加载下一页），数据量不大的时候，可以直接注释
					page : param.page || 1
				// 当前页
				};
			},
			processResults : function(data, param) {
				param.page = data.nextPage;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo,
		// 显示查询结果
		templateSelection : function(obj) {
			$("#companyName").val(obj.name);
			return obj.name || obj.text;
		}
	});
	$("#select2-cpSelect2-container").html(companyName);
}

// 家庭性别
function initSex(relate) {
	if (relate == null || relate == "") {
		var optionDom = buildSex('');
		$("#eFamSex0").append(optionDom);
	} else {
		$.each(relate, function(index) {
			var sex = "";
			sex = relate[index].eFamSex; // 关系信息
			var optionDom = buildSex(sex);
			$("#eFamSex" + index).append(optionDom);
		})
	}
}

function buildSex(sex) {
	var optionDom = ""
	$.each(dicsSex, function(index) {
		if (sex == dicsSex[index].dictValue) {
			optionDom += "<option selected='selected' value='" + dicsSex[index].dictValue + "' " + ">" + dicsSex[index].dictDestination + "</option>";
		} else {
			optionDom += "<option value='" + dicsSex[index].dictValue + "' " + ">" + dicsSex[index].dictDestination + "</option>";
		}
	});
	return optionDom;
}

// 关系信息
function initFY(relate) {
	$("*[name='eFamRelation'] option").remove();
	if (relate == null || relate == "") {
		var optionDom = buildFY('');
		$("#eFamRelation0").append(optionDom);
	} else {
		$.each(relate, function(index) {
			var fam = "";
			fam = relate[index].eFamRelation; // 关系信息
			var optionDom = buildFY(fam);
			$("#eFamRelation" + index).append(optionDom);
		})
	}
}

function buildFY(gx) {
	var optionDom = "";
	optionDom = "<option></option>"
	$.each(familRelate, function(index) {
		if (gx == familRelate[index].dictValue) {
			optionDom += "<option selected='selected' value='" + familRelate[index].dictValue + "' " + ">" + familRelate[index].dictDestination + "</option>";
		} else {
			optionDom += "<option value='" + familRelate[index].dictValue + "' " + ">" + familRelate[index].dictDestination + "</option>";
		}
	});
	return optionDom;
}

// 开户银行信息
function initOpenBank(result) {
	$("#bank0[name='bank'] option").remove();
	var bankInfo = result; // 开户银行结果
	var optionDom = "";
	if (bankInfo == null || bankInfo == "") {
		optionDom = buildOpenBank('');
	} else {
		optionDom = buildOpenBank(result);
	}
	$("#bank0").append(optionDom);
}

function buildOpenBank(bank) {
	var optionDom = "";
	optionDom = "<option></option>"
	$.each(openBank, function(index) {
		if (bank == openBank[index].dictDestination) {
			optionDom += "<option selected='selected' value='" + openBank[index].dictDestination + "' " + ">" + openBank[index].dictDestination + "</option>";
		} else {
			optionDom += "<option value='" + openBank[index].dictDestination + "' " + ">" + openBank[index].dictDestination + "</option>";
		}
	});
	return optionDom;
}

// 学历信息
function initED(result) {
	$("*[name='eDegree'] option").remove();
	var education = result; // 学历结果
	if (education == null || education == "") {
		var optionDom = buildED('');
		$("#eDegree0").append(optionDom);
	} else {
		$.each(education, function(index) {
			var degree = "";
			degree = education[index].eDegree; // 学历
			var optionDom = buildED(degree);
			$("#eDegree" + index).append(optionDom);
		})
	}
}

function buildED(xl) {
	var optionDom = "";
	optionDom = "<option></option>"
	$.each(graduation, function(index) {

		if (xl == graduation[index].dictValue) {
			optionDom += "<option selected='selected' value='" + graduation[index].dictValue + "' " + ">" + graduation[index].dictDestination + "</option>";
		} else {
			optionDom += "<option value='" + graduation[index].dictValue + "' " + ">" + graduation[index].dictDestination + "</option>";
		}
	});
	return optionDom;
}

// ************************面板信息展开收缩*****************************************//
function showBase(obj) {
	if ($("#jbxx").is(':hidden')) {
		$("#jbxx").show();
		$(obj).html("收起");
	} else {
		$("#jbxx").hide();
		$(obj).html("展开");
	}
}
function showEd(obj) {
	if ($("#jyxx").is(':hidden')) {
		$("#jyxx").show();
		$(obj).html("收起");
		$("#edClass").addClass("ks-active");
	} else {
		$("#edClass").removeClass("ks-active");
		$("#jyxx").hide();
		$(obj).html("展开");
	}
}
function showWork(obj) {
	if ($("#gzjl").is(':hidden')) {
		$("#gzjl").show();
		$(obj).html("收起");
		$("#wkClass").addClass("ks-active");
	} else {
		$("#wkClass").removeClass("ks-active");
		$("#gzjl").hide();
		$(obj).html("展开");
	}
}
function showFamily(obj) {
	if ($("#jtxx").is(':hidden')) {
		$("#jtxx").show();
		$(obj).html("收起");
		$("#fyClass").addClass("ks-active");
	} else {
		$("#fyClass").removeClass("ks-active");
		$("#jtxx").hide();
		$(obj).html("展开");
	}
}

// 工资卡信息面板显示与隐藏
function showWageCard(obj) {
	if ($("#wageCardPanel").is(':hidden')) {
		$("#wageCardPanel").show();
		$(obj).html("收起");
		$("#wageCardClass").addClass("ks-active");
	} else {
		$("#wageCardClass").removeClass("ks-active");
		$("#wageCardPanel").hide();
		$(obj).html("展开");
	}
}